package cn.tedu.crm.customer.service.impl;

import cn.tedu.crm.customer.mapper.*;
import cn.tedu.crm.customer.pojo.dto.CustomerAddParam;
import cn.tedu.crm.customer.pojo.dto.CustomerFollowUpParam;
import cn.tedu.crm.customer.pojo.dto.CustomerInsertParam;
import cn.tedu.crm.customer.pojo.dto.CustomerQuery;
import cn.tedu.crm.customer.pojo.entity.*;
import cn.tedu.crm.customer.pojo.vo.CustomerListVO;
import cn.tedu.crm.customer.pojo.vo.DistrictVO;
import cn.tedu.crm.customer.pojo.vo.EditCustomerVO;
import cn.tedu.crm.customer.service.CustomerService;
import cn.tedu.crm.deposit.pojo.vo.CustomerVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

import static cn.tedu.crm.base.controller.FileController.fileImgPath;

@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Autowired
    private CustomerFollowUpMapper customerFollowUpMapper;

    @Autowired
    private  FollowImgMapper followImgMapper;

    @Autowired
    private HouseMapper houseMapper;



    @Autowired
    private DistrictMapper districtMapper;


    /**
     * 新增客户信息
     * @param param
     */
    @Override
    public void addCustomer(CustomerAddParam param) {
        log.debug("【新增客户基本信息】业务，param参数：{}", param);
        Customer customer = new Customer();
        BeanUtils.copyProperties(param, customer);

        if (customer.getId()==null){
            log.debug("开始【新增客户基本信息】业务，customer");
            customerMapper.insert(customer);
            log.debug("结束【新增客户基本信息】业务，customer参数：{}", customer);

            //新增客户跟进
            log.debug("开始【新增客户基本信息】业务，customerFollowUp");
            Boolean addCustomerFollowUp = saveCustomerFollowUp(param,customer.getId());
            log.debug("【新增客户跟进】业务，customerFollowUp参数：{}", addCustomerFollowUp);

            //新增客户跟进附件信息
            log.debug("开始【新增客户附件信息】业务，param");
            Boolean addFollowImg = saveFollowImg(param,customer.getId());
            log.debug("结束【新增客户附件信息】业务，followImg参数：{}", addFollowImg);

            //新增量房信息
            log.debug("开始【新增量房信息】业务，param");
            Boolean addHouse = saveHouse(param,customer.getId());
            log.debug("结束【新增量房信息】业务，followImg参数：{}", addHouse);

        }else {
            log.debug("开始【更新客户基本信息】业务customer");
            customerMapper.updateById(customer);
            log.debug("结束【更新客户基本信息】业务，customer参数：{}", customer);

        }

    }

    /**
     * 新增量房信息
     * @param param
     * @param id
     * @return
     */
    private Boolean saveHouse(CustomerAddParam param, Long id) {
        House house = new House();
        BeanUtils.copyProperties(param, house);
        house.setCustomerId(id);
        houseMapper.insert(house);
        return true;
    }


    /**
     * 新增客户跟进附件信息
     * @param param
     * @param customerId
     * @return
     */
    private Boolean saveFollowImg(CustomerAddParam param, Long customerId) {
        log.debug("【新增客户FollowImg】业务，param参数：{}", param);
        FollowImg followImg = new FollowImg();
        log.debug("获取图片路径" + fileImgPath);

        log.debug("查询FollowImg,customerId 参数{}", customerId);
            log.debug("开始【新增附件】FollowImg");
            for (int i = 0 ;i<fileImgPath.size(); i++){
                followImg.setImgUrl(fileImgPath.get(i));
                followImg.setCustomerId(customerId);
                followImgMapper.insertImg(followImg);
            }
            followImg.setCustomerId(customerId);
            log.debug("结束【新增附件】FollowImg");
        return  true;
    }

    /**
     * 新增客户跟进信息
     * @param param
     * @param customerId
     * @return
     */
    private Boolean saveCustomerFollowUp(CustomerAddParam param ,Long customerId) {
        log.debug("【新增客户customerFollowUp】业务，param参数：{}", param);

        CustomerFollowUp customerFollowUp = new CustomerFollowUp();
        BeanUtils.copyProperties(param ,customerFollowUp);
            log.debug("查询FollowUpId，customerId参数{}", customerId);
            log.debug("开始【新增客户customerFollowUp】CustomerFollowUp:{}",customerFollowUp);
            customerFollowUp.setCustomerId(customerId);
            customerFollowUpMapper.insert(customerFollowUp);
        log.debug("结束【新增客户customerFollowUp】CustomerFollowUp:{}",customerFollowUp);
        return  true;
    }


    /**
     * 查询客户信息
     * @param query
     * @return
     */
    public List<CustomerListVO> selectCustomerList(CustomerQuery query) {
        log.debug("查询客户信息业务query{}",query);
        List<CustomerListVO> customerListVOS = customerMapper.selectCustomerList(query);
        log.debug("查询客户信息业务 customerListVOS");
        for (CustomerListVO  item: customerListVOS  ){
            List<String> imgUrl = getImgUrl(item);
            item.setImgUrl(imgUrl);
            //log.debug("查询客户信息业务customerListVOS{}",customerListVOS);
        }
        return customerListVOS;
    }

    /**
     * 获取图片路径
     * @param customerListVO
     * @return
     */
    private List<String> getImgUrl(CustomerListVO customerListVO){
        Long customerId = customerListVO.getId();
        return  followImgMapper.selectUrlById(customerId);
    }

    /**
     * 删除客户信息
     * @param id
     */

    @Override
    public void deleteCustomer(Long id) {
        log.debug("删除用户信息业务{}",id);
        customerMapper.deleteById(id);
        customerFollowUpMapper.deleteByCustomerId(id);
        followImgMapper.deleteByCustomerId(id);
        houseMapper.deleteByCustomerId(id);
    }

        /**
     * 查询修改客户信息
     * @param query
     * @return
     */
    @Override
    public List<EditCustomerVO> SelectEditCustomer(CustomerQuery query) {
        log.debug("查询修改客户信息业务query{}",query);
        List<EditCustomerVO> list = customerMapper.SelectEditCustomer(query);
        return list;
    }

    /**
     * 查询客户地区信息
     * @param district
     * @return
     */
    @Override
    public List<DistrictVO> selectDistrict(District district) {
        List<DistrictVO> list = districtMapper.selectDistrict(district);
        return list;
    }



    @Override
    public void updateFollowUp(CustomerFollowUpParam param) {
        log.debug("param参数{}",param );
        CustomerFollowUp customerFollowUp = new CustomerFollowUp();
        BeanUtils.copyProperties(param,customerFollowUp);
        log.debug("customerFollowUp参数{}",customerFollowUp );
        customerFollowUpMapper.updateFollowUp(customerFollowUp);
        House house = new House();
        BeanUtils.copyProperties(param,house);
        log.debug("house参数{}",house );
        houseMapper.updateByCustomerId(house);

    }


    /**id
     * 删除客户信息时通过客户id删除附件
     * @param imgUrl
     */
    @Override
    public void deleteImg(String imgUrl) {
        String img = imgUrl.substring(imgUrl.lastIndexOf("/20",imgUrl.length()-1));
        followImgMapper.deleteByCustomerImgUrl(img);
    }


    //定金表查询
    @Override
    public CustomerVO selectId(Long id) {
        log.debug("定金表查询id{}",id);
        return customerMapper.selectIds(id);
    }

    //移动端新增客户
    @Override
    public void insertCustomer(CustomerInsertParam customerInsertParam) {
        customerMapper.insertCustomer(customerInsertParam);
    }

    //移动端新增客户跟进
    @Override
    public void insertCustomerFollowUp(CustomerInsertParam customerInsertParam) {
        customerFollowUpMapper.insertCustomerFollowUp(customerInsertParam);
    }


}
